<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller 
{

    /*登录页面*/
    public function index()
    {
        $code = $_GET['code'];//获取code
        //var_dump($code);exit();
        if(!empty($code))
        {	
			//授权的
            $appModer = M('App');
            $res       = $appModer->select();
            $appid = trim($res[0]['appid']);
            $secret = trim($res[0]['appsecret']);
			
			//网页授权获取用户openid Start
			$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
			$weixin     = file_get_contents($url);//通过code换取网页授权access_token		   
			$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
			$array      = get_object_vars($jsondecode);//转换成数组
			$openid     = $jsondecode->openid;//输出openid
			//网页授权获取用户openid End
			//var_dump($openid);exit;
			//写入openid

			
 			$arr = unserialize(F('data.php'));
			$nowTime = time();
			if($nowTime-$array['shijian']>3600 || $array['shijian'] == '')
			{
				//获取获取用户信息的token Start
				$shijian = time();
				$get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid .'&secret='.$secret;
				$get_token_json = file_get_contents($get_token_url);
				$get_token_obj = json_decode($get_token_json);
				$get_token_array = get_object_vars($get_token_obj);
				//var_dump($get_token_array);die;
				$token = $get_token_array['access_token'];
				//var_dump($token);die;
				$data = array(
					'token' => $token,
					'time' => time(),
				);
				//获取获取用户信息的token End
				F('data.php',$data);
				
			}else{
				$token = $arr['token'];
			} 
			
			//获取信息的
			
			
			
            $url1 = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$openid.'&lang=zh_CN';
			
 			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
			curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
			curl_setopt($ch, CURLOPT_TIMEOUT, 15);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
			$output = curl_exec($ch);
			curl_close($ch);
			$jsondecode1 = json_decode($output); 
			$array1 = get_object_vars($jsondecode1);
            //var_dump($array1);exit;		

			$userData['head_pic'] = $array1['headimgurl'];
			
			$userData['nickname'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $array1['nickname']);
			
			//$userData['nickname'] = $array1['nickname'];
			$userData['openid'] = $openid;
			$u['user_Id'] = session('user_id');
			
			$Model = new \Think\Model();
			$new_point = $Model->execute("update user set head_pic= '".$array1['headimgurl']."', nickname ='".$array1['nickname']."', openid ='".$openid."' where user_id = ".session('user_id'));
			
            $userModel    = M('user');
            $use['user_Id']= session('user_id');
            $resUser          = $userModel->where($use)->limit(1)->select();
			//var_dump($resUser);die;
            if($resUser)
            {
                session('user_id',$resUser['0']['user_id']);
                session('openid',$resUser['0']['openid']);
				$this->redirect('/Admin/Index/index');
                //$this->success('您已登录！', __APP__.'/Admin/Index/index');
           }
        }else{
	    	session('user_id', 0);
			session('openid', 0);
			session_destroy();		
		}
		
       //$this->assign('openid', $openid);
		$this->display();
    }

    /*登录*/
    public function signIn()
    {
        $arr['phone'] = trim(I('post.phone'));
        $arr['pwd']   = md5(I('post.pwd'));      
        $UserModer    = D('User');      
        $res          = $UserModer->checkUse($arr['phone'], $arr['pwd']);
				
        if($res)
        {	
            $UserModer->points($res['0']['user_id']);
            session('user_id',$res['0']['user_id']);	
            //session('openid',$res['0']['openid']);	
            $this->success('登录成功！', __APP__.'/Admin/Index/index');
        }else
        {
            $this -> error('输入的电话号码或密码不正确，请重新输入');
        }
    }

    /*注册*/
    public function register()
    {
		
		$this->assign('tuid',$_GET['uid']);
        $this->display();
    }
    /*注册1*/
    public function register1()
    {
        
        $this->assign('tsid',$_GET['sid']);
        $this->display();
    }
	
	/*注册2*/
    public function register2()
    {
		
		$this->assign('tuid',$_GET['uid']);
        $this->display();
    }
    //直接注册会员
    public function registerIn()
    {
        $use['phone'] = trim(I('post.phone'));
        $use['pwd']   = md5(I('post.pwd'));
        $use['pid'] = trim(I('post.tuid'));
        $use['name'] = trim(I('post.name'));

        $UserModer    = D('User');
        $res          = $UserModer->insertUse($use['phone'], $use['pwd'], $use['pid'], $use['name']);

        if ($res == 'phone')
        {
            $this->success('电话格式错误！', 'register');
            
        }elseif ($res == 'one')
        {
            $this -> error('输入的电话号码已被注册！', 'register');
            
        }elseif($res == 'no')
        {
            $this -> error('注册失败', 'register');
        }elseif($res == 'same')
        {
            $this -> error('请重新选择上级', 'register');
        }else
		{
			$this->success('注册成功，可以进行登录！','index');
		}
    }

    public function registerIn1()
    {
        $use['phone'] = trim(I('post.phone'));
        $use['pwd']   = md5(I('post.pwd'));
        $use['pid'] = trim(I('post.tsid'));
        $use['is_user'] = 0;
        $use['name'] = trim(I('post.name'));
        
        $UserModer    = D('User');
        $res          = $UserModer->insertUse1($use['phone'], $use['pwd'], $use['pid'], $use['is_user'], $use['name']);

        if ($res == 'phone')
        {
            $this->success('电话格式错误！', 'register');
            
        }elseif ($res == 'one')
        {
            $this -> error('输入的电话号码已被注册！', 'register');
            
        }elseif($res == 'no')
        {
            $this -> error('注册失败', 'register');
        }elseif($res == 'same')
        {
            $this -> error('请重新选择上级', 'register');
        }else
        {
            $this->success('注册成功！','watch');
        }
    }
	
	public function registerIn2()
    {
        $use['phone'] = trim(I('post.phone'));
        $use['pwd']   = md5(I('post.pwd'));
        $use['pid'] = trim(I('post.tuid'));
        $use['is_user'] = 1;
        $use['name'] = trim(I('post.name'));
		

        $UserModer    = D('User');
        $res          = $UserModer->insertUse1($use['phone'], $use['pwd'], $use['pid'],$use['is_user'], $use['name']);

        if ($res == 'phone')
        {
            $this->success('电话格式错误！', 'register');
            
        }elseif ($res == 'one')
        {
            $this -> error('输入的电话号码已被注册！', 'register');
            
        }elseif($res == 'no')
        {
            $this -> error('注册失败', 'register');
        }elseif($res == 'same')
        {
            $this -> error('请重新选择上级', 'register');
        }else
		{
			$this->success('注册成功！','watch');
		}
    }
	
	/*注册填写上下级*/
    public function register10()
    {
        $this->display();
    }
	public function registerIn10()
    {
        $use['phone'] = trim(I('post.phone'));
        $use['pwd']   = md5(I('post.pwd'));
        $use['name'] = trim(I('post.name'));
        $use['leverPhone'] = trim(I('post.leverPhone'));
        $use['leverType'] = trim(I('post.leverType'));
		
        $UserModer    = D('User');
        $res          = $UserModer->insertUse10($use['phone'], $use['pwd'], $use['name'], $use['leverPhone'], $use['leverType']);
		
		if ($res == 'data')
        {
            $this->success('注册成功，可以进行登录！','index');
            
        }elseif ($res == 'udata')
        {
            $this->success('注册成功，可以进行登录！','index');
            
        }elseif ($res == 'sdata')
        {
            $this->success('注册成功，可以进行登录！','index');
            
        }elseif ($res == 'noLever')
        {
            $this -> error('您输入的上级不存在,请核对!', 'register10');
            
        }elseif ($res == 'err')
        {
            $this -> error('您填写的电话号码格式不正确!', 'register10');
            
        }elseif ($res == 'one')
        {
            $this -> error('输入的电话号码已被注册!', 'register10');
            
        }
    }
	
	/*公众号关注*/
    public function watch()
    {
        $this->display();
    }
}

